import store from '../store'
import Q from 'q'

export default {

  moneyFormate(number, places, symbol, thousand, decimal) {
    number = number || 0;
    places = !isNaN(places = Math.abs(places)) ? places : 2;
    symbol = symbol !== undefined ? symbol : '¥';
    thousand = thousand || ',';
    decimal = decimal || '.';
    var negative = number < 0 ? '-' : '',
      i = parseInt(number = Math.abs(+number || 0).toFixed(places), 10) + '',
      j = (j = i.length) > 3 ? j % 3 : 0;
    return symbol + negative + (j ? i.substr(0, j) + thousand : '') + i.substr(j).replace(/(\d{3})(?=\d)/g, '$1' + thousand) + (places ? decimal + Math.abs(number - i).toFixed(places).slice(2) : '');
  },

  //获取用户类型
  getUserByType(userType) {

    let userTypename = '';
    // console.log(userType, 'userType')
    try {
      userType = decodeURIComponent(userType)
    } catch (e) {}

    if (userType == 'YX#') {
      userTypename = '意向客户'
    } else if(userType == 'YB#') {
      userTypename = '一般客户'
    } else if(userType == 'YCJ') {
      userTypename = '成交客户'
    } else if(userType == 'WX#') {
      userTypename = '无意向客户'
    } else if(userType == 'QT#') {
      userTypename = '其他客户'
    } else if(userType == 'NA#') {
      userTypename = '无效客户'
    } else {
      userTypename = '一般客户'
    }

    return userTypename;
  },


  formateStr (str) {
    const limit = 10
    return typeof str === 'string' && str.length > limit ? str.substr(0, limit) + '...' : str
  },

  getStatename (state) {
    let statename = ''
    try {
      state = decodeURIComponent(state)
    } catch (e) {}

    if (state === 'INT') {
      statename = '初步接洽'
    } else if (state === 'XQ#') {
      statename = '到访'
    } else if (state === 'FA#') {
      statename = '报价'
    } else if (state === 'TP#') {
      statename = '签约'
    } else if (state === 'CJ#') {
      statename = '成交'
    } else if (state === 'NC#') {
      statename = '新客户'
    } else if (state === 'ED#') {
      statename = '更新资料'
    } else if (state === 'GH#') {
      statename = '关怀'
    } else if (state === 'YY#') {
      statename = '预约'
    } else if (state === 'DF#') {
      statename = '到访'
    } else if (state === 'FS#') {
      statename = '首次签约'
    } else if (state === 'XB#') {
      statename = '续报签约'
    } else {
      statename = '新客户'
    }
    return statename
  },


  getFollowStatename(state){
    return this.clueType(state);

  },

  getParentname(name) {
    return name ? name: '未知名称'
  },

  getTime(time) {
    return time ? time : '无跟进时间'
  },

  safeNickName (name) {
    return name
  },
  //  格式化生日
  formateBirthDay (value) {
    if (!value) {
      return '未知出生日期'
    }
    const dateArr = value.split('/')
    if (dateArr[1] && dateArr[1].length < 2 && dateArr[1] < 10) dateArr[1] = '0' + dateArr[1]
    if (dateArr[2] && dateArr[2].length < 2 && dateArr[2] < 10) dateArr[2] = '0' + dateArr[2]
    return dateArr.join('-')
  },
  babyGroupPrefix (babyname) {
    return '宝宝：' + (babyname || '')
  },
  getUserSource(sourceId, custom_type) {
    if (!custom_type) custom_type = 0
    let sources = store.state.app.customSources
    let sourceName = ''

    if (!sourceId && custom_type === 0) {
      sourceName = '微信'
    } else {
      sourceId = parseInt(sourceId)
      console.log(JSON.stringify(sources), sourceId, sources[sourceId])
      if (typeof sources[sourceId] !== 'undefined') {
        sourceName = sources[sourceId]
      } else {
        if(custom_type === 0) {
          sourceName = '微信'
        } else if(sources.length) {
          sourceName = sources[0]
        } else {
          sourceName = '未知来源'
        }
      }
    }

    return sourceName
  },
  clueType (clueType) {
    let clueTypeDesc = '新客户'
    try {
       clueType = decodeURIComponent(clueType)
    } catch (e) {}

    if (clueType === 'NC#') {
      clueTypeDesc = '新客户'
    } else if (clueType === 'ED#') {
      clueTypeDesc = '更新资料'
    } else if (clueType === 'GH#') {
      clueTypeDesc = '关怀'
    } else if (clueType === 'YY#') {
      clueTypeDesc = '预约'
    } else if (clueType === 'DF#') {
      clueTypeDesc = '到访'
    } else if (clueType === 'FS#') {
      clueTypeDesc = '首次签约'
    } else if (clueType === 'XB#') {
      clueTypeDesc = '续报签约'
    }
    return clueTypeDesc
  },
  getBizType (type) {
    let bizType = '未知操作'
    try {
       type = decodeURIComponent(type)
    } catch (e) {}

    if (type === 'LQ#') {
      bizType = '领取红包'
    } else if (type === 'TX#') {
      bizType = '提现'
    }
    return bizType
  },
  getFlowType (type) {
    let getFlowType = ''
    try {
       type = decodeURIComponent(type)
    } catch (e) {}

    if (type === 'IN#') {
      getFlowType = '+'
    } else if (type === 'OUT') {
      getFlowType = '-'
    }
    return getFlowType
  }

}
